package com.shangguan.springboot.config;

import com.shangguan.springboot.realm.UserRealm;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName: ShiroConfig
 * @Description: shiro配置
 * @Author: 一尘
 * @Date: 2020 年 03 月 09 21:02
 * @Version 1.0
 */
@Configuration
public class ShiroConfig {

    /**
     * 创建Realm
     */
    @Bean(name="userRealm")
    public UserRealm getRealm(){
        return new UserRealm();
    }

    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联realm
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    /**
     * Shiro内置过滤器，可以实现权限相关的拦截器
     *    常用的过滤器：
     *       anon: 无需认证（登录）可以访问
     *       authc: 必须认证才可以访问
     *       user: 如果使用rememberMe的功能可以直接访问
     *       perms： 该资源必须得到资源权限才可以访问
     *       role: 该资源必须得到角色权限才可以访问
     */
        @Bean
         public ShiroFilterChainDefinition shiroFilterChainDefinition(){
            DefaultShiroFilterChainDefinition definition = new DefaultShiroFilterChainDefinition();
               // definition.addPathDefinition("/doLogin","perms[user:login]");
                definition.addPathDefinition("/doLogin","anon");
                definition.addPathDefinition("/unicode","anon");
                definition.addPathDefinition("/logout","logout");
                definition.addPathDefinition("/admin/**","perms[user:login,user:list]");
                definition.addPathDefinition("/member/**","perms[user:login,user:list]");
               /*definition.addPathDefinition("/css/**","anon");
               definition.addPathDefinition("/js/**","anon");
               definition.addPathDefinition("/images/**","anon");
               definition.addPathDefinition("/fonts/**","anon");
               definition.addPathDefinition("/lib/**","anon");*/
            definition.addPathDefinition("/**","authc");
           return  definition;
         }

    /**
     * 凭证匹配器
     * （由于我们的密码校验交给Shiro的SimpleAuthenticationInfo进行处理了
     * 所以我们需要修改下doGetAuthenticationInfo中的代码;
     * ）
     *
     * @return
     *//*
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher() {
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        //散列算法:这里使用MD5算法;
        hashedCredentialsMatcher.setHashAlgorithmName("md5");
        //散列的次数，比如散列两次，相当于 md5(md5(""));
        hashedCredentialsMatcher.setHashIterations(2);
        return hashedCredentialsMatcher;
    }*/

}
